<?php

namespace Modules\Test\Admin;

use App\Service\TableService;
use Duxravel\Core\Events\ManageForm;
use Duxravel\Core\UI\Form;
use Duxravel\Core\UI\Form\Text;
use Duxravel\Core\UI\Widget;
use Illuminate\Support\Facades\Storage;
use Modules\Test\Model\Test;

class Lists extends \Modules\System\Admin\Expend
{

    public string $model = Test::class;

    protected $collectData = null;

    public function __construct(){
        $data = [];
        for ($i=0;$i<10;$i++){
            $data[$i]['id'] = $i+1;
            $data[$i]['name'] = '测试名称_' . ($i + 1);
            $data[$i]['datetime'] = time()+($i + 1) * random_int(10,100);
            $data[$i]['text'] = '文字描述_' . ($i + 1);
            $data[$i]['image'] = Storage::url('/upload/2022-01-09/v8oTrgbo7MJyABiIgI6SU42qtJ95kEtIiYbLpxD8.png');
            $data[$i]['status'] = random_int(1,5);
            $data[$i]['tag'] = random_int(1,5);
            $data[$i]['toggle'] = random_int(0,1);
            $data[$i]['progress'] = random_int(0,5);
        }
        $this->collectData = collect($data);
    }


    protected function table()
    {
        return TableService::make($this->collectData,function (TableService $table){
            //action
            $table->showAction('admin.wechat.setting.page','添加','dialog','plus');
            $menu = $table->action()->menu('更多操作','primary');
            $menu->link('添加', 'admin.wechat.setting.page')->type('dialog');
//            $menu->link('导出', 'admin.wechat.setting.del');
//            $table->attr('scroll',['x'=>2000]);
            $table->column("id","id")->sorter();
            $table->column("用户","name")->image('image');
            $table->column("文本内容","text");
            $table->column("日期时间","datetime")->date("Y-m-d H:i:s");
            $table->showImage('图片','image');

            $column = $table->column('操作')->fixed()->width('100px');
//            $column->link('编辑', 'admin.wechat.setting.page', ['id' => 'id'])->type('dialog')->icon('edit');
//            $column->link('删除', 'admin.wechat.setting.del', ['id' => 'id'])
//                ->type('ajax', ['method' => 'post'])
//                ->icon('delete')
//                ->class("arco-link-status-danger")->attr('vIf','rowData.record.id!=1');

            $column->menu('编辑', 'admin.wechat.setting.page', ['id' => 'id']);
//            $column->menu("删除",'admin.wechat.setting.del', ['id' => 'id']);
//            $table->batch();
//            $table->batch()->button('批量删除', 'primary','admin.wechat.setting.del');
            $table->batch()->select('批量删除', 'admin.wechat.setting.del');
//            $widget = Widget::menu("更多操作",'default',function (Widget\Menu $menu){
//                $menu->link('删除', 'admin.wechat.setting.del', ['id' => 'id'])
//                    ->type('ajax', ['method' => 'post'])
//                    ->icon('delete')
//                    ->class("arco-link-status-danger")->attr('vIf','rowData.record.id!=1');
//            });
//            $table->column('操作1','id')->node($widget);

            //filter
//        $table->filter('配置名称', 'name')->text('请输入配置名称搜索')->quick();
//        $table->filter('标题', 'title', function ($query, $value) {
//            $query->where('title', 'like', '%'.$value.'%');
//        })->text('请输入标题进行搜索');
//        $table->filter('标题2', 'title')->text('请输入标题内容', function(Text $text) {
//            $text->afterText('test');
//        });
//        $table->filter('搜索日期', 'date')->date( '请选择日期');
//        $table->filter('搜索日期时间', 'datetime')->datetime( '请选择日期时间');
//        $table->filter('搜索日期时间范围', 'daterange')->daterange( '请选择日期时间范围');
//        $table->filter('下拉选择', 'select')->select([
//            0 => '选项一',
//            1 => '选项二',
//        ]);
//        $options = [
//            ['id' => 1,'name' => '选项一','pid' => 0],
//            ['id' => 2,'name' => '选项二','pid' => 0],
//            ['id' => 3,'name' => '选项三','pid' => 0],
//            ['id' => 4,'name' => '子选项一','pid' => 1],
//            ['id' => 5,'name' => '子选项二','pid' => 1],
//            ['id' => 6,'name' => '子选项三','pid' => 2],
//            ['id' => 7,'name' => '子选项四','pid' => 2],
//        ];
//        $table->filter('级联选择', 'cascader')->cascader($options);
//        $table->filterParams('status','all');
//        $table->filterType('全部',function ($query){
//            $query->where("id",1);
//        });
//        $table->filterType('处理中');
//        $table->filterType('待处理');
//        $table->filterType('失败');
//        $table->filterParams('status','all')->filterType('全部');
//        $table->filterParams('status',0)->filterType('启用');
//        $table->filterParams('status',1)->filterType('禁用');
        });
    }

    public function form(int $id = 0): Form
    {
        $form = new Form($this->collectData);
        $form->title("添加微信配置");
        $form->setKey('id', $id);
        $form->text('配置名称', 'name')->must();
        $tab = $form->tab();
        $row = $form->info();
        $tab->column('小程序配置',function (Form $form) use ($row) {
            $values = ['app_id'=>'','secret'=>'','token'=>'','aes_key'=>''];
            if($row){
                $values = $row->getAttribute('mini_app');
            }
            $form->text('小程序appid', 'mini_app_app_id')->value($values['app_id']);
            $form->text('小程序secret', 'mini_app_secret')->value($values['secret']);
            $form->text('小程序token', 'mini_app_token')->value($values['aes_key']);
            $form->text('小程序aes_key', 'mini_app_aes_key')->value($values['app_id'])->prompt('EncodingAESKey，兼容与安全模式下请一定要填写！！！');
        });
        $tab->column('公众号配置',function (Form $form) use ($row) {
            $values = ['app_id'=>'','secret'=>'','token'=>'','aes_key'=>''];
            if($row){
                $values = $row->getAttribute('official_account');
            }
            $form->text('公众号appid', 'official_account_app_id')->value($values['app_id']);
            $form->text('公众号secret', 'official_account_secret')->value($values['secret']);
            $form->text('公众号token', 'official_account_token')->value($values['token']);
            $form->text('公众号aes_key', 'official_account_aes_key')->value($values['aes_key'])->prompt('EncodingAESKey，兼容与安全模式下请一定要填写！！！');
        });
        $tab->column('支付配置',function (Form $form) use ($row) {
            $values = ['mch_id'=>'','key'=>'','key_path'=>'','cert_path'=>''];
            if($row){
                $values = $row->getAttribute('payment');
            }
            $form->text('商户号', 'payment_mch_id')->value($values['mch_id']);
            $form->text('支付密钥', 'payment_key')->value($values['key'])->help("32位密钥",true);
            $form->file('证书apiclient_key.pem', 'payment_key_path')->value($values['key_path'])->fileUrl($values['key_path'])->type('manage');
            $form->file('证书apiclient_cert.pem', 'payment_cert_path')->value($values['cert_path'])->fileUrl($values['cert_path'])->type('manage');
        });
        $tab->column('开放平台配置',function (Form $form) use ($row) {

        });
        $tab->column('企业微信配置',function (Form $form) use ($row) {

        });
        $form->before(function($data, $time, $model) {
            $mini_app = [];
            $official_account = [];
            $payment = [];
            foreach ($data as $key=>$value){
                if(str_contains($key,'mini_app')){
                    $mini_app[str_replace('mini_app_','',$key)] = $value?:'';
                }elseif(str_contains($key,'official_account')){
                    $official_account[str_replace('official_account_','',$key)] = $value?:'';
                }elseif(str_contains($key,'payment')){
                    $payment[str_replace('payment_','',$key)] = $value?:'';
                }
            }
            $model->mini_app = $mini_app;
            $model->official_account = $official_account;
            $model->payment = $payment;
        });
        return $form;
    }

}
